﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using InternalManagementSystem.DAO.Models;
using InternalManagementSystem.DAO;
using System.Data;
using System.Text;

/// <summary>
///ViolationNoticeBLL 的摘要说明
/// </summary>
public class ViolationNoticeBLL
{
    Permission permission = null;

    public ViolationNoticeBLL()
    {
    }

    public ViolationNoticeBLL(Permission permission)
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
        this.permission = permission;
    }

    #region 违章违纪情况进行记录、编辑和删除


    /// <summary>
    /// 获取公司
    /// </summary>
    /// <param name="ViolationNoticeid"></param>
    /// <returns></returns>
    public ViolationNotice GetViolationNotice(int ViolationNoticeid)
    {
        ViolationNoticeDAO dao = new ViolationNoticeDAO();
        return dao.LoadViolationNotice(ViolationNoticeid);
    }

    /// <summary>
    /// 获取违章列表
    /// </summary>
    /// <returns></returns>
    public IList<ViolationNotice> GetAllViolationNotice()
    {
        ViolationNoticeDAO dao = new ViolationNoticeDAO();
        return dao.LoadAllViolationNotices().Cast<ViolationNotice>().ToList();
    }

    #region 获取视图


    public DataSet GetViolationNoticeView(int ViolationNoticeid)
    {
        DBHelper helper = new DBHelper();
        string sql = "select * from v_ViolationNotice where id = " + ViolationNoticeid.ToString();
        return helper.ExecuteQuerySQL(sql);
    }

    public DataSet GetAllViolationNoticeView(string condition)
    {
        DBHelper helper = new DBHelper();
        string sql = "select * from v_ViolationNotice where 1=1 " + condition;
        return helper.ExecuteQuerySQL(sql);
    }

    public DataSet GetAllViolationNoticeView()
    {
        DBHelper helper = new DBHelper();
        string sql = "select * from v_ViolationNotice" ;
        return helper.ExecuteQuerySQL(sql);
    }

    /// <summary>
    /// 可以通过时间查询、
    /// 单位查询 姓名 证件号、
    /// 显示全年违章情况、
    /// 显示以前违章情况、
    /// 通过输入、
    /// 等信息查询。
    /// </summary>
    /// <param name="keyword"></param>
    /// <returns></returns>
    public string GetKeyWordCondition(string keyword)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(" and (UserName like '%" + keyword + "%'");
        sb.Append(" or Credential like '%" + keyword + "%'");
        sb.Append(" or CompanyName like '%" + keyword + "%')");
        return sb.ToString();
    }

    public string GetCommitDateCondition(string dateString)
    {
        return " and CommitDate > Convert(datetime,'" + dateString + "')";
    }

    #endregion

    /// <summary>
    /// 获取员工违章列表
    /// </summary>
    /// <returns></returns>
    public IList<ViolationNotice> GetAllViolationNotice(int userid,int companyid)
    {
        ViolationNoticeDAO dao = new ViolationNoticeDAO();
        return dao.LoadAllViolationNotices("CompanyID = " + companyid.ToString() + " and UserID =" + userid.ToString() , string.Empty)
            .Cast<ViolationNotice>()
            .ToList();
    }

    /// <summary>
    /// 获取公司违章列表
    /// </summary>
    /// <returns></returns>
    public IList<ViolationNotice> GetAllViolationNotice(int companyid)
    {
        ViolationNoticeDAO dao = new ViolationNoticeDAO();
        return dao.LoadAllViolationNotices("CompanyID = " + companyid.ToString(), string.Empty)
            .Cast<ViolationNotice>()
            .ToList();
    }

    /// <summary>
    /// 新增公司
    /// </summary>
    /// <param name="ViolationNotice"></param>
    /// <returns></returns>
    public bool AddViolationNotice(ViolationNotice ViolationNotice)
    {

        bool result = false;
        if (permission.IsViolationNoticeCreate)
        {
            try
            {
                ViolationNoticeDAO dao = new ViolationNoticeDAO();
                dao.InsertViolationNotice(ViolationNotice);
                result = true;
            }
            catch
            {
                
            }
        }
        return result;
    }

    /// <summary>
    /// 更新公司
    /// </summary>
    /// <param name="ViolationNotice"></param>
    /// <returns></returns>
    public bool UpdateViolationNotice(ViolationNotice ViolationNotice)
    {
        bool result = false;
        if (permission.IsViolationNoticeEdit)
        {
            try
            {
                ViolationNoticeDAO dao = new ViolationNoticeDAO();
                dao.UpdateViolationNotice(ViolationNotice);
                result = true;
            }
            catch
            {
               
            }
        }
        return result;
    }

    /// <summary>
    /// 删除公司
    /// </summary>
    /// <param name="ViolationNoticeid"></param>
    /// <returns></returns>
    public bool DeleteViolationNotice(int ViolationNoticeid)
    {
        bool result = false;
        if (permission.IsViolationNoticeEdit)
        {
            try
            {
                ViolationNoticeDAO dao = new ViolationNoticeDAO();
                dao.DeleteViolationNotice(ViolationNoticeid);
                result = true;
            }
            catch
            {
                
            }
        }
        return result;
    }



    #endregion


    public DataSet GetStaffDeduct(int vid)
    {
        DBHelper helper = new DBHelper();
       return  helper.ExecuteQuerySQL("select * from v_StaffDeduct where ViolationNID = " + vid.ToString());

    }

    public DataSet GetCompanyDeduct(int vid)
    {
        DBHelper helper = new DBHelper();
        return helper.ExecuteQuerySQL("select * from v_CompanyDeduct where ViolationNID = " + vid.ToString());

    }
}
